/*  Bilingual - Language learning software
 *  Copyright (C) 2014  Claudio Vellage; Daupayev Nursultan
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

package bilingual.learn;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;

/**
 *
 * @author Claudio
 */
public class FXMLDocumentController implements Initializable {
    
    @FXML
    TextArea SourceTextArea;
    
    @FXML
    TextArea TargetTextArea;
    
    @FXML
    ComboBox<Language> LanguageSelector;
    ObservableList<Language> languageData = FXCollections.observableArrayList();
        
    @FXML
    ComboBox<Lesson> LessonSelector;
    ObservableList<Lesson> lessonData = FXCollections.observableArrayList();
    
    @FXML
    private void selectedNewLanguage() throws IOException {
        FileReader sourceLanguageFile = new FileReader();
        FileReader targetLanguageFile = new FileReader();
        
        if (LessonSelector.getSelectionModel().getSelectedItem() != null && LanguageSelector.getSelectionModel().getSelectedItem() != null) {
        sourceLanguageFile.setFileName(
                "data/" 
                + LanguageSelector.getSelectionModel().getSelectedItem().getSourceLanguage() 
                + "/lesson" + LessonSelector.getSelectionModel().getSelectedItem().getLessonNumber() 
                + ".txt"
        );
        targetLanguageFile.setFileName(
                "data/" 
                + LanguageSelector.getSelectionModel().getSelectedItem().getTargetLanguage() 
                + "/lesson" + LessonSelector.getSelectionModel().getSelectedItem().getLessonNumber() 
                + ".txt"
        );
        
        SourceTextArea.setText(sourceLanguageFile.getFileContent());
        TargetTextArea.setText(targetLanguageFile.getFileContent());
        } else if (LessonSelector.getSelectionModel().getSelectedItem() == null) {
            SourceTextArea.setText("Please choose a lesson.");
            TargetTextArea.setText("Please choose a lesson.");
        } else if (LanguageSelector.getSelectionModel().getSelectedItem() == null) {
            SourceTextArea.setText("Please choose a language.");
            TargetTextArea.setText("Please choose a language.");
        } else {
            SourceTextArea.setText("Please choose a language and lesson.");
            TargetTextArea.setText("Please choose a language and lesson.");
        }
    }
    
    @FXML
    private void selectedNewLesson() throws IOException {
        FileReader sourceLanguageFile = new FileReader();
        FileReader targetLanguageFile = new FileReader();
        
        if (LessonSelector.getSelectionModel().getSelectedItem() != null && LanguageSelector.getSelectionModel().getSelectedItem() != null) {
        sourceLanguageFile.setFileName(
                "data/" 
                + LanguageSelector.getSelectionModel().getSelectedItem().getSourceLanguage() 
                + "/lesson" + LessonSelector.getSelectionModel().getSelectedItem().getLessonNumber() 
                + ".txt"
        );
        targetLanguageFile.setFileName(
                "data/" 
                + LanguageSelector.getSelectionModel().getSelectedItem().getTargetLanguage() 
                + "/lesson" + LessonSelector.getSelectionModel().getSelectedItem().getLessonNumber() 
                + ".txt"
        );
        
        SourceTextArea.setText(sourceLanguageFile.getFileContent());
        TargetTextArea.setText(targetLanguageFile.getFileContent());
        } else if (LessonSelector.getSelectionModel().getSelectedItem() == null) {
            SourceTextArea.setText("Please choose a lesson.");
            TargetTextArea.setText("Please choose a lesson.");
        } else if (LanguageSelector.getSelectionModel().getSelectedItem() == null) {
            SourceTextArea.setText("Please choose a language.");
            TargetTextArea.setText("Please choose a language.");
        } else {
            SourceTextArea.setText("Please choose a language and lesson.");
            TargetTextArea.setText("Please choose a language and lesson.");
        }
    }
       
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        /* Add user guidance */
        SourceTextArea.setText("Please choose a language and lesson.");
        TargetTextArea.setText("Please choose a language and lesson.");
        
        /* Initialization of available Languages */
        languageData.add(new Language("German","English"));
        languageData.add(new Language("English","German"));
        
        /* Initialization of available Lessons */
        lessonData.add(new Lesson(1));
        
        /* Assign data to comboBoxes */
        LanguageSelector.setItems(languageData);
        LessonSelector.setItems(lessonData);
    }    
    
}
